package dao;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description TODO
 * @Author hhy
 * @Date 2020/9/17 10:57
 */
public class BookList {
    List<String> myborrowBook = new ArrayList<>();  //我手里有的书
    String book[] = {"斗破苍穹","斗罗大陆","朝花夕拾","晚熟的人","中国史"};
    int bookNum[] = {1,2,3,4,5}; //每本书的库存



    public void getBookList(){
        for (int i = 0; i < book.length; i++) {
            System.out.println(i+".\t\t\t"+book[i]+"\t\t\t\t"+bookNum[i]);
        }
        System.out.println(book.length+".返回上一级");
    }

    //借书
    public String borrow(int num){
        if (isBookNumNull(num)){
            bookNum[num]--;  //库存减一
            return book[num];
        }
        return "借书失败";
    }

    //书还有库存吗
    public boolean isBookNumNull(int num){
        if (bookNum[num]>0){
            return true;
        }
        return false;
    }

    //借到的书存起来
    public void myBorrowBook(int bookId){
        myborrowBook.add(bookId+"");
    }

    //我手里借到的书
    public void myBorrowBook(){
        if (myborrowBook.size()==0) {
            System.out.println("你现在还没有借书哦~，快去借书系统看看吧");
            return;
        }
        for (int i = 0; i < myborrowBook.size(); i++) {
            System.out.print(Integer.valueOf(myborrowBook.get(i))+"."+book[Integer.valueOf(myborrowBook.get(i))]);
        }
        System.out.println();
    }

    //我借了书吗
    public boolean myBorrowBookNum(){
        if (myborrowBook.size()==0) return false;
        return true;
    }

    //还书 还书就是自己的书减少，仓库里的书增加
    public void returnBook(int num){
        myborrowBook.remove(num+""); //自己的书减少
        bookNum[num]++; //仓库是书增加；
    }



    //单例模式来避免库存无法刷新的问题
    private static BookList instance;

    private BookList() {
    }

    public static synchronized BookList getInstance() {
        if (instance == null) {
            instance = new BookList();
        }
        return instance;
    }
}
